package com.minshenglife.zanzan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.ArticleDao;
import com.minshenglife.zanzan.dao.MaterialDao;
import com.minshenglife.zanzan.entity.weixin.ArticleTodb;
import com.minshenglife.zanzan.entity.weixin.Material;
import com.minshenglife.zanzan.service.MaterialService;

/**
 * 微信素材service
 * @author jianghaibo
 *
 */
@Component
@Transactional
public class MaterialServiceImpl implements MaterialService{

	@Resource
	MaterialDao materialDao;
	@Resource
	ArticleDao articleDao;

	@Override
	public Material get(Long materialId) {
		return materialDao.get(materialId);
	}

	@Override
	public Material add(Material material) {
		Long id = materialDao.findMaxId();
		if(id != null){
			id += 1l;
		}else{
			id = 1l;
		}
		material.setId(id);
		materialDao.add(material);
		return material;
	}

	@Override
	public void update(Material material) {
		materialDao.update(material);
	}

	@Override
	public void delete(Material material) {
		materialDao.delete(material.getId());
	}

	@Override
	public List<Material> findAll(Material material) {
		List<Material> materials = materialDao.findByExample(material);
		for (Material material2 : materials) {
			if(material2 != null && material2.getMaterialType() != null && material2.getMaterialType().equals("news")){
				ArticleTodb articleTodb = new ArticleTodb();
				articleTodb.setMaterialId(material2.getId());
				List<ArticleTodb> articleList = articleDao.findByExample(articleTodb);
				material2.setArticleList(articleList);
			}
			
		}
		return materials;
	}
}
